package com.becca.cake.service.payment.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.becca.cake.core.excel.excelWriter.ExcelInfo;
import com.becca.cake.core.excel.excelWriter.impl.ExcelWriteHandle;
import com.becca.cake.core.exception.ServiceException;
import com.becca.cake.dao.payment.PaymentDAO;
import com.becca.cake.model.order.OrderModel;
import com.becca.cake.model.payment.InvoiceModel;
import com.becca.cake.model.payment.OrderRefundModel;
import com.becca.cake.model.payment.PaymentModel;
import com.becca.cake.service.payment.IPaymentService;
import com.becca.cake.util.DateUtils;
import com.becca.cake.util.JsonUtil;

/**
 * 财务查询service实现类
 * @copyright  尊星(上海)信息科技有限公司
 * @author yellow
 * @date 2016年5月20日 上午10:27:12
 */
@Service
public class PaymentServiceImpl implements IPaymentService {
    @Autowired
    private PaymentDAO paymentDAO;

    @Override
    public List<PaymentModel> selectPayments(Map<String, Object> map) throws Exception {
        return this.paymentDAO.selectPayments(map);
    }

    @Override
    public Long selectPaymentsCount(Map<String, Object> map) throws Exception {
        return this.paymentDAO.selectPaymentsCount(map);
    }

    @Override
    public List<InvoiceModel> selectInvoices(Map<String, Object> map) throws ServiceException {
        String orderStatus = OrderModel.getInvoiceOrderStatus();
        map.put("orderStatus", orderStatus);
        return this.paymentDAO.selectInvoices(map);
    }

    @Override
    public Long selectInvoicesCount(Map<String, Object> map) throws ServiceException {
        String orderStatus = OrderModel.getInvoiceOrderStatus();
        map.put("orderStatus", orderStatus);
        return this.paymentDAO.selectInvoicesCount(map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updIsReferenceInvoice(String jsonList) throws ServiceException {
        List<InvoiceModel> list = JsonUtil.returnObjectList(jsonList, InvoiceModel.class);
        this.paymentDAO.updIsReferenceInvoice(list);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public void exportInvoicesToExcel(HttpServletRequest request, HttpServletResponse response,
                                      String orderIds) throws ServiceException {
        //step1：查询发票信息
        List list = this.paymentDAO.selectOrderInvoicesById(orderIds);

        ExcelInfo info = new ExcelInfo();
        info.setColumnHeader("订单编号 ,订单编码 ,发票抬头,发票内容 ,发票金额,收货人 ,收货电话,收货地址");
        info.setFeilds("id,orderCode,invoiceTitle,invoiceContent,payAmount,consignee,consignPhone,consignAddress");
        info.setDatas(list);
        info.setSheetName("发票单信息");
        info.setFileName(DateUtils.getStringToday() + "发票单");
        ExcelWriteHandle.getInstance().writeEcxcel(info, request, response);
    }

    @Override
    public List<OrderRefundModel> selectOrderRefunds(Map<String, Object> map)
                                                                             throws ServiceException {
        String orderStatus = OrderModel.getRefusedOrderStatus();
        map.put("orderStatus", orderStatus);
        return this.paymentDAO.selectOrderRefunds(map);
    }

    @Override
    public Long selectOrderRefundsCount(Map<String, Object> map) throws ServiceException {
        String orderStatus = OrderModel.getRefusedOrderStatus();
        map.put("orderStatus", orderStatus);
        return this.paymentDAO.selectOrderRefundsCount(map);
    }
}
